package com.guigu.qingcheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.domain.pojo.TbBrand;
import com.guigu.qingcheng.domain.entity.PageResult;
import com.guigu.qingcheng.mapper.TbBrandMapper;
import com.guigu.qingcheng.service.TbBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-03
 */
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand> implements TbBrandService {

    @Override
    public PageResult<TbBrand> findPage(Integer page, Integer size) {
        //创建page对象
        Page<TbBrand> pageBrand = new Page<>(page,size);
        baseMapper.selectPage(pageBrand, null);
        long total = pageBrand.getTotal();
        List<TbBrand> brandList = pageBrand.getRecords();
        return new PageResult<>(total, brandList);
    }

    @Override
    public PageResult<TbBrand> findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbBrand> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbBrand> pageBrand = new Page<>(page,size);
        baseMapper.selectPage(pageBrand,wrapper);
        return new PageResult<>(pageBrand.getTotal(), pageBrand.getRecords());
    }


    // 根据查询条件封装模块对象
    private QueryWrapper<TbBrand> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name")))
                wrapper.like("name",searchMap.get("name"));
        }
        return wrapper;
    }

}
